package com.whty.cms.filewps.common.service.impl;

import com.whty.cms.filewps.common.service.MongoService;
import com.whty.cms.filewps.util.Pagination;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MongoServiceImpl implements MongoService{
	@Autowired
	private MongoOperations mongoOperations;

	@Override
	public <T> void insert(T entity) throws Exception {
		mongoOperations.insert(entity);
		
	}

	@Override
	public <T> void insert(T entity, String collectionName) throws Exception {
		mongoOperations.insert(entity, collectionName);
		
	}

	@Override
	public <T> T findOne(Query query, Class<T> clazz) throws Exception {
		return mongoOperations.findOne(query, clazz);
	}

	@Override
	public <T> T findOne(Query query, Class<T> clazz, String collectionName) throws Exception {
		return mongoOperations.findOne(query, clazz, collectionName);
	}

	@Override
	public <E> List<E> list(Query query, Class<E> clazz) throws Exception {
		return mongoOperations.find(query, clazz);
	}
	
	@Override
	public <E> List<E> list(Query query, Class<E> clazz, String collectionName) throws Exception {
		return mongoOperations.find(query, clazz, collectionName);
	}

	@Override
	public <T> long count(Query query, Class<T> clazz) throws Exception {
		return mongoOperations.count(query, clazz);
	}

	@Override
	public long count(Query query, String collectionName) throws Exception {
		return mongoOperations.count(query, collectionName);
	}

	@Override
	public <E> List<E> listForPagination(Pagination pagination, Query query, Class<E> clazz) throws Exception {
		Integer count = Long.valueOf(mongoOperations.count(query, clazz)).intValue();
		if (pagination == null) {
            pagination = new Pagination();
        }
        pagination.setTotalCount(count);
        query.skip(pagination.getFirstRow()).limit(pagination.getLastRow());
        return mongoOperations.find(query, clazz);
	}

	@Override
	public <E> List<E> listForPagination(Pagination pagination, Query query, Class<E> clazz, String collectionName)
			throws Exception {
		Integer count = Long.valueOf(mongoOperations.count(query, collectionName)).intValue();
        if (pagination == null) {
            pagination = new Pagination();
        }
        pagination.setTotalCount(count);
        query.skip(pagination.getFirstRow() - 1).limit(pagination.getNumPerPage());
        return mongoOperations.find(query, clazz, collectionName);
	}

	@Override
	public <T> void remove(Query query, Class<T> clazz) throws Exception {
		mongoOperations.remove(query, clazz);
		
	}

	@Override
	public <T> void remove(Query query, String collectionName) throws Exception {
		mongoOperations.remove(query, collectionName);
		
	}

	@Override
	public <T> void updateFirst(Query query, Update update, Class<T> clazz) throws Exception {
		mongoOperations.updateFirst(query, update, clazz);
		
	}

	@Override
	public void updateFirst(Query query, Update update, String collectionName) throws Exception {
		mongoOperations.upsert(query, update, collectionName);
		
	}

	@Override
	public <T> void updateMulti(Query query, Update update, Class<T> clazz) throws Exception {
		mongoOperations.updateMulti(query, update, clazz);
		
	}

}
